# Replication Code and Data for Paper "Cross-National Measures of the Intensity of COVID-19 Public Health Policies"

This zip file contains R code and data necessary to reproduce results in our paper. It is important to note that some of the steps are computationally intensive; reproducing the aggregated dataset requires a node with approximately 500GB of RAM due to the size of the time-series version of our policy data.

Two R packages aren't available that need to be installed separately.

First, `cmdstanr` is the back-end to the Stan MCMC engine. You can install it with the following command:

```
install.packages("cmdstanr", repos = c("https://mc-stan.org/r-packages/", getOption("repos")))
```

Then load the package and install Stan with:

```
install_cmdstan()
```

Then if you have the `remotes` package installed, you can install `idealstan` with:

```
remotes::install_github("saudiwin/idealstan", ref="b942f3e")
```

Using this specific commit ensures the code base is the same as that which was used to run the measurement model in the paper.

## System Specification

This model has been estimated on both a Mac OS X (12.6.1) system and a Linux Ubuntu server. It has not been tested on Windows.

## Scripts

A description of the scripts is as follows:

**NB: The scripts assumes that the R home directory has been set to the working folder containing the scripts/data.**

1. `ag_dataset.R` This script loads the policy record CoronaNet data from the `data/coronanet_internal_allvars.rds` file. It converts this compressed record storage format into a conventional time-series cross-section dataset with one row per country per day per policy record. It creates separate datasets for each of the six policy intensity scores we estimate. These are denoted by the labels `biz` = Business Restrictions, `sd` = Social Distancing, `ht` = Health Technology, `hr` = Health Resources, `school` = School Restrictions, `mask` = Mask Restrictions, `hm` = Health Management. Note that Health Technology and Health Management are combined into one policy score index, which is denoted as `hm2` in the data. These aggregated datasets are saved as `index_long_model_` with policy type suffixes. Oxford data is merged in as well to the long dataset format.

2. `run_indices.R` Using the environmental variable `MODELTYPE`, this script loads one of the datasets that match one of the policy types described previously. This script then fits the IRT ideal point model described in the paper and saves the fitted model object along with some `ggplot2` plots. It is currently designed to be run on a dedicated node on a cluster with a minimum of 28 cores (most recent version used 128 cores). This will take approximately 2-3 days to run per index.

3. `analyze_indices.R` This script loads the fitted model object for each policy type and produces descriptive plots for the paper. It also checks convergence criteria. The script saves ideal point estimates for each index as RDS files. This script also uses the helper scripts `apply_draws.R` and `summarize_draws_mc.R`.

4. `inference.R` This script loads the indices and fits the exploratory regression models reported in the paper. 

5. `sim_index.R` This script runs the model simulation described in the paper.

6. `analyze_sim_index.R` This script loads the results of the simulation and creates plots for the paper.

7. `me_ovb_contact_cov_centered.stan` This file contains the Stan code to fit a regression model with measurement error in our indices as we describe in the paper.

8. `contrib_heatmap.R` This file generates the list of contributions for our paper.

9. `evaluate_metrics.R` This file does the K-fold cross-validation of the measurement model as described in the paper.

The aggregated datasets used to run the measurement models are saved as `coronanet/index_long_model_`MODELTYPE`.rds`. All model fits are saved as `coronanet/activity_fit_`MODELTYPE`random_walk_run_1_RR.rds`. The estimated index values are in `indices/all_indices.rds` or `indices/all_indices.csv`. The measurement regression fit is saved as `coronanet/contact_fixmod.rds`.

## Auxiliary code files with additional functions:

1. `apply_draws.R`, `create_items.R`, `create_items_long.R`, 